In [13]:
print range(10)
By default range
starts at the value 0 (as seen above) and goes till one value before the value specified to it.
We can give it an explicit starting value as well
In [14]:
print range(2, 10)
One more possible variation is to specify the step size between successive values
In [17]:
print range(0, 101, 10) #Step size of 10
In [12]:
for i in range(10):
print i
If we wanted to print the square of each number in the list?
In [18]:
list1 = range(10)
for i in list1:
print i ** 2
In [4]:
name = 'Alice'
for c in name:
print c
In [5]:
tup = (1, 2, 3, 4)
for i in tup:
print i
Printing all the keys of a dictionary
In [6]:
d = {}
d['foo'] = 1
d['bar'] = 2
d['baz'] = 3
for k in d:
print k
Notice the keys are not printed in the same order in which they were added to the dict. Dictionaries do not maintain order
Printing the values of the dictionary
In [8]:
for v in d.values():
print v
Printing both key and values in a dictionary
In [10]:
for k, v in d.items():
print k, v
In [11]:
i = 10
while i > 0:
print i
i = i -1
In [ ]: